package com.telit.springfox.documentation.swagger2.mappers;///*
// *
// *  Copyright 2015 the original author or authors.
// *
// *  Licensed under the Apache License, Version 2.0 (the "License");
// *  you may not use this file except in compliance with the License.
// *  You may obtain a copy of the License at
// *
// *         http://www.apache.org/licenses/LICENSE-2.0
// *
// *  Unless required by applicable law or agreed to in writing, software
// *  distributed under the License is distributed on an "AS IS" BASIS,
// *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// *  See the License for the specific language governing permissions and
// *  limitations under the License.
// *
// *
// */
//
//package com.telit.springfox.documentation.swagger2.mappers;
//
//import com.google.common.base.Function;
//import com.google.common.collect.Maps;
//import com.google.common.collect.Multimap;
//import io.swagger.models.Contact;
//import io.swagger.models.Operation;
//import io.swagger.models.Tag;
//import io.swagger.models.*;
//import io.swagger.models.properties.Property;
//import org.mapstruct.Mapper;
//import org.mapstruct.Mapping;
//import org.mapstruct.Mappings;
//import springfox.documentation.schema.ModelReference;
//import springfox.documentation.service.*;
//import springfox.documentation.swagger2.mappers.*;
//
//import java.util.*;
//
//import static com.google.common.collect.FluentIterable.from;
//import static com.google.common.collect.Lists.newArrayList;
//import static com.google.common.collect.Maps.*;
//import static springfox.documentation.builders.BuilderDefaults.nullToEmptyList;
//import static springfox.documentation.swagger2.mappers.ModelMapper.modelRefToProperty;
//
///**
// * 调整swagger 使展开类级别方法接口时 按照描述字符串排序
// */
//@Mapper(uses = {ModelMapper.class, ParameterMapper.class, SecurityMapper.class, LicenseMapper.class, VendorExtensionsMapper.class})
//public abstract class ServiceModelToSwagger2Mapper {
//
//    @Mappings({
//            @Mapping(target = "info", source = "resourceListing.info"),
//            @Mapping(target = "paths", source = "apiListings"),
//            @Mapping(target = "host", source = "host"),
//            @Mapping(target = "schemes", source = "schemes"),
//            @Mapping(target = "definitions", source = "apiListings"),
//            @Mapping(target = "securityDefinitions", source = "resourceListing"),
//            @Mapping(target = "securityRequirement", ignore = true),
//            @Mapping(target = "security", ignore = true),
//            @Mapping(target = "swagger", ignore = true),
//            @Mapping(target = "parameters", ignore = true),
//            @Mapping(target = "responses", ignore = true),
//            @Mapping(target = "externalDocs", ignore = true),
//            @Mapping(target = "vendorExtensions", source = "vendorExtensions")
//    })
//    public abstract Swagger mapDocumentation(Documentation from);
//
//    @Mappings({
//            @Mapping(target = "license", source = "from",
//                    qualifiedBy = {LicenseMapper.LicenseTranslator.class, LicenseMapper.License.class}),
//            @Mapping(target = "contact", source = "from.contact"),
//            @Mapping(target = "termsOfService", source = "termsOfServiceUrl"),
//            @Mapping(target = "vendorExtensions", source = "vendorExtensions")
//    })
//    protected abstract Info mapApiInfo(ApiInfo from);
//
//    protected abstract Contact map(springfox.documentation.service.Contact from);
//
//    @Mappings({
//            @Mapping(target = "description", source = "notes"),
//            @Mapping(target = "operationId", source = "uniqueId"),
//            @Mapping(target = "schemes", source = "protocol"),
//            @Mapping(target = "security", source = "securityReferences"),
//            @Mapping(target = "responses", source = "responseMessages"),
//            @Mapping(target = "vendorExtensions", source = "vendorExtensions"),
//            @Mapping(target = "externalDocs", ignore = true)
//    })
//    protected abstract Operation mapOperation(springfox.documentation.service.Operation from);
//
//    @Mappings({
//            @Mapping(target = "externalDocs", ignore = true),
//            @Mapping(target = "vendorExtensions", ignore = true)
//    })
//    protected abstract Tag mapTag(springfox.documentation.service.Tag from);
//
//    protected List<Scheme> mapSchemes(List<String> from) {
//        return from(from).transform(toScheme()).toList();
//    }
//
//    protected List<Map<String, List<String>>> mapAuthorizations(
//            Map<String, List<AuthorizationScope>> from) {
//        List<Map<String, List<String>>> security = newArrayList();
//        for (Map.Entry<String, List<AuthorizationScope>> each : from.entrySet()) {
//            Map<String, List<String>> newEntry = newHashMap();
//            newEntry.put(each.getKey(), from(each.getValue()).transform(scopeToString()).toList());
//            security.add(newEntry);
//        }
//        return security;
//    }
//
//    protected Map<String, Response> mapResponseMessages(Set<ResponseMessage> from) {
//        Map<String, Response> responses = newTreeMap();
//        for (ResponseMessage responseMessage : from) {
//            Property responseProperty;
//            ModelReference modelRef = responseMessage.getResponseModel();
//            responseProperty = modelRefToProperty(modelRef);
//            Response response = new Response()
//                    .description(responseMessage.getMessage())
//                    .schema(responseProperty);
//            response.setExamples(Maps.<String, Object>newHashMap());
//            response.setHeaders(transformEntries(responseMessage.getHeaders(), toPropertyEntry()));
//            Map<String, Object> extensions = new VendorExtensionsMapper()
//                    .mapExtensions(responseMessage.getVendorExtensions());
//            response.getVendorExtensions().putAll(extensions);
//            responses.put(String.valueOf(responseMessage.getCode()), response);
//        }
//        return responses;
//    }
//
//    private EntryTransformer<String, Header, Property> toPropertyEntry() {
//        return new EntryTransformer<String, Header, Property>() {
//            @Override
//            public Property transformEntry(String key, Header value) {
//                Property property = modelRefToProperty(value.getModelReference());
//                property.setDescription(value.getDescription());
//                return property;
//            }
//        };
//    }
//
//    protected Map<String, Path> mapApiListings(Multimap<String, ApiListing> apiListings) {
//        Map<String, Path> paths = new LinkedHashMap<>();
//        for (ApiListing each : apiListings.values()) {
//            List<ApiDescription> apis = each.getApis();
//            // 按照描述字符串 进行排序
//            apis.sort(Comparator.comparing(bean -> bean.getOperations().get(0).getSummary()));
//            for (ApiDescription api : apis) {
//                paths.put(api.getPath(), mapOperations(api, Optional.fromNullable(paths.get(api.getPath()))));
//            }
//        }
//        return paths;
//    }
//
//    private Function<AuthorizationScope, String> scopeToString() {
//        return new Function<AuthorizationScope, String>() {
//            @Override
//            public String apply(AuthorizationScope input) {
//                return input.getScope();
//            }
//        };
//    }
//
//    private Path mapOperations(ApiDescription api, Optional<Path> existingPath) {
//        Path path = existingPath.or(new Path());
//        for (springfox.documentation.service.Operation each : nullToEmptyList(api.getOperations())) {
//            Operation operation = mapOperation(each);
//            path.set(each.getMethod().toString().toLowerCase(), operation);
//        }
//        return path;
//    }
//
//
//    private Function<String, Scheme> toScheme() {
//        return new Function<String, Scheme>() {
//            @Override
//            public Scheme apply(String input) {
//                return Scheme.forValue(input);
//            }
//        };
//    }
//
//}
